import {
  Component,
  OnInit,
  AfterViewInit,
  ViewChild,
  ElementRef,
  ViewChildren,
  QueryList,
  EventEmitter,
  Output,
  Input
} from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';


@Component({
  selector: 'bx-input',
  templateUrl: './inputDel.component.html',
  styleUrls: ['./inputDel.component.scss'],
})

export class InputDelComponent implements OnInit, AfterViewInit {
  @ViewChild("InputFocus")
  childCmps1: ElementRef;
  valueceshi = "";
  @Output() inputvalueChange = new EventEmitter();
  @Output() bxBlur = new EventEmitter();
  @Output() submit = new EventEmitter();
  @Input() bxplaceholder = "";
  @Input() style1 = "";
  @Input() bxmax = "";

  @Input()
  get inputvalue() {
    return this.valueceshi;
  }

  set inputvalue(val) {
    this.valueceshi = val;
    this.inputvalueChange.emit(this.valueceshi);
  }

  constructor(private _message: NzMessageService) {

  }
  checkValue(e) {

  }
  formsubmit(e) {
    e.preventDefault();
    //this.submit.emit();
  }

  typeBlur(e) {
    /*this.inputvalue = this.subString(this.inputvalue,8);
    this.inputvalueChange.emit(this.inputvalue);*/
    if (this.getLength(this.inputvalue) > 8) {
      this._message.create('error', '分类名称限制中文最长4位，英文8位');
      this.childCmps1.nativeElement.focus();
      return false;
    }
    this.bxBlur.emit(this.inputvalue);
  }
  getLength(str) {
    var l = str.length;
    var blen = 0;
    for (let i = 0; i < l; i++) {
      if ((str.charCodeAt(i) & 0xff00) != 0) {
        blen++;
      }
      blen++;
    }
    return blen;
  }
  subString(str, len, hasDot = false) {
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex, "**").length;
    for (let i = 0; i < strLength; i++) {
      singleChar = str.charAt(i).toString();
      if (singleChar.match(chineseRegex) != null) {
        newLength += 2;
      }
      else {
        newLength++;
      }
      if (newLength > len) {
        break;
      }
      newStr += singleChar;
    }


    if (hasDot && strLength > len) {
      newStr += "...";
    }
    return newStr;
  }
  ngOnInit() {
    console.log('mmm')
  }
  ceshi(e) {
    console.log(e.target.value);
  }
  ngAfterViewInit() {
    this.childCmps1.nativeElement.focus();
  }

  prevent(e) {
    e.preventDefault();
  }

  valueDelete(e) {
    this.inputvalue = "";
  }
}
